<template>
  <el-card class="m-8">

    <section>

      <!-- <section slot="header" class="mb-4 flex items-center">
        <el-button class="mr-4" type="primary" @click="handleAdd()">新增</el-button>
      </section> -->


      <el-table :data="tableList" align="center" border class="mt-4" stripe style="width: 100%">
        <el-table-column label="经验金币" prop="exp_coin"/>
        <el-table-column label="宝箱概率" prop="box_rate"/>
        <el-table-column label="是否开启" prop="is_open">
          <template slot-scope="{row}">
            <el-tag v-if="row.is_open === 1" type="success" size="normal"  effect="dark">开启</el-tag>
            <el-tag v-else type="warning" size="normal"  effect="dark">关闭</el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="添加时间" prop="created_at"/>

        <el-table-column align="center" fixed="right" label="操作">
          <template slot-scope="{row}">
            <el-button :loading="detailLoading" plain type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" plain @click="handleSDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <cus-pagination
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="meta.total"
        @pagination="initData"
      />
    </section>
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="dialogTitle"
      @close="handleCloseDialog('form')"
      width="30%"
    >
      <el-form
        ref="form"
        :model="formData"
        :rules="Rules"
        label-position="right"
        label-width="120px"
      >

        <el-form-item label="经验金币" prop="exp_coin">
          <el-input v-model="formData.exp_coin" placeholder="请输入奖励ID" size="normal" clearable></el-input>
        </el-form-item>
        <el-form-item label="宝箱概率" prop="box_rate">
          <el-input v-model="formData.box_rate" placeholder="请输入宝箱概率" size="normal" clearable>
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否开启" prop="is_open">
          <el-radio-group v-model="formData.is_open">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="2">关闭</el-radio>
          </el-radio-group>
          
        </el-form-item>
        
      </el-form>
      <footer slot="footer">
        <el-button type="danger" @click="dialogFormVisible = false">取消</el-button>
        <el-button type="success" @click="handleOnSubmit">保存</el-button>
      </footer>
    </el-dialog>

  </el-card>
</template>

<script>
import {
  gameLotteryLadderSetting,
  gameLotteryLadderSettingSave
} from '@/api/compositeManagement'
// 礼物下拉数据
import { getData } from '@/api/roomGift/giftList'

export default {
  data() {
    return {
      dialogEditVisible: false,
      options: [
        { val: 0, name: '禁用' },
        { val: 1, name: '正常' }
      ],
      title: '新增',
      listQuery: {
        page: 1,
        perPage: 10
      },
      formData: {
        id: undefined,
        exp_coin: undefined,
        box_rate: undefined,
        is_open: 1
      },
      Rules: {
        exp_coin: [{ required: true, message: '请输入', trigger: 'blur' }],
        box_rate: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      tableList: [],
      meta: {
        page: 1,
        perPage: 10,
        total: 0
      },

      /* 弹出框开关 */
      dialogFormVisible: false,

      detailLoading: false,
      isEdit: false,
      rewardType: [
        {
          value: 'prop',
          label: '道具'
        },
        {
          value: 'pet-prop',
          label: '宠物道具'
        },
      ],
      dialogTitle: '新增'
    }
  },
  created() {
    this.initData()
  },
  methods: {
    handleSDelete(row) {
      
      this.$confirm('此操作不可恢复，是否继续删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        gameLotteryLadderPrizeDel({
          id: row.id
        }).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          }
          this.dialogFormVisible = false
          this.initData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleOnSubmit() {

      this.$refs['form'].validate(valid => {
        if (valid) {

          for (let key in this.formData) {
            this.formData[key] = !this.formData[key] ? undefined : this.formData[key]
          }
          gameLotteryLadderSettingSave(this.formData).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.dialogFormVisible = false
              this.initData()
            }
          })

        }
      })
    },
    handleCloseDialog(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
        this.$refs[formName].resetFields()
      }
      this.formData = {
        id: undefined,
        exp_coin: undefined,
        box_rate: undefined,
        is_open: 1
      }
    },

    handleAdd() {
      this.dialogFormVisible = true
      this.isEdit = false
      this.dialogTitle = '新增'
    },
    handleEdit(row) {
      this.isEdit = true
      this.dialogTitle = '编辑'
      this.formData = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },

    async initData() {
      try {
        const { data } = await gameLotteryLadderSetting({
          ...this.listQuery
        })
        this.tableList = [data]
      } catch (e) {
        console.error(e)
      }
    },
    handleChange() {
      this.formData.reward_id = undefined
      this.formData.count = undefined
    },

  }
}
</script>

<style scoped>
.search {
  padding-left: 70px;
}

.el-input {
  width: 400px;
}

::v-deep .el-upload-list__item {
  transition: none !important;
}

::v-deep.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
</style>
